package com.yangxf.e_historyQuery;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.junit.Test;

public class HistoryQueryTest {

	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	
	/**
	 * 查询历史的流程实例
	 */
	@Test
	public void findHistoryProcessInstance() {
		String processInstanceId = "37501";
		
		HistoricProcessInstance hpi = processEngine.getHistoryService() // 与历史表相关的service 
			.createHistoricProcessInstanceQuery() // 创建历史流程实例查询
			.processInstanceId(processInstanceId) // 使用流程实例id查询
			.orderByProcessInstanceStartTime()
			.asc()
			.singleResult();
		
		System.out.println(hpi.getId() + " " + hpi.getProcessDefinitionId() + " " + hpi.getStartTime() + " " + hpi.getEndTime() + " " + hpi.getDurationInMillis());
	}
	
	/**
	 * 查询历史活动
	 */
	@Test
	public void findHistoryActiviti() {
		
		String processInstanceId = "37501";
		List<HistoricActivityInstance> list = processEngine.getHistoryService() // 历史相关service
			.createHistoricActivityInstanceQuery() // 创建历史活动实例查询
			.processInstanceId(processInstanceId)
			.orderByHistoricActivityInstanceStartTime() // 
			.asc()
			.list();
		
		if(list!=null && list.size()>0) {
			for(HistoricActivityInstance hai : list) {
				System.out.println(hai.getId()+" "+hai.getProcessInstanceId()+" "+
						hai.getActivityType()+" "+hai.getStartTime()+" "+hai.getEndTime()+" "+
						hai.getDurationInMillis());
				System.out.println("========================================================");
			}
		}
		
	}
	
	
	/**
	 * 查询历史任务
	 */
	@Test
	public void findHistoryTask() {
		
		String processInstanceId = "37501";
		
		List<HistoricTaskInstance> list = processEngine.getHistoryService() // 历史数据相关的service
			.createHistoricTaskInstanceQuery() // 历史任务查询对象
			.processInstanceId(processInstanceId) // 
			.orderByHistoricTaskInstanceStartTime()
			.asc()
			.list();
		
		if(list!=null && list.size()>0) {
			for(HistoricTaskInstance hti : list) {
				System.out.println(hti.getId()+" "+hti.getName()+" "+hti.getProcessInstanceId()+" "+hti.getStartTime()+" "+hti.getEndTime());
				System.out.println("===========================================================");
			}
		}
	}
	
	/**
	 * 查询历史流程变量
	 */
	@Test
	public void findHistoryProcessVariable() {
		
		String processInstanceId = "37501";
		
		List<HistoricVariableInstance> list = processEngine.getHistoryService()
			.createHistoricVariableInstanceQuery() // 历史变量流程查询对象
			.processInstanceId(processInstanceId)
			.orderByProcessInstanceId()
			.asc()
			.list();
		
		if(list!=null && list.size()>0) {
			for(HistoricVariableInstance hvi : list) {
				System.out.println(hvi.getId()+" "+
						hvi.getProcessInstanceId()+" "+hvi.getVariableName()+" "+
						hvi.getVariableTypeName()+" "+hvi.getValue());
				System.out.println("###############################################");
			}
		}
	}
	
}
